﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="KnownExtensionsConfigCollection.cs" company="" />
// <summary>
//   The configuration collection of known extensions and their properties.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Xompare.Configuration
{
    using System;
    using System.Configuration;

    /// <summary>
    /// The configuration collection of known extensions and their properties.
    /// </summary>
    public class KnownExtensionsConfigCollection : ConfigurationElementCollection
    {
        public KnownExtensionsConfigCollection()
        {
            AddElementName = "Extension";
        }

        #region Implementation of ConfigurationElementCollection

        protected override ConfigurationElement CreateNewElement()
        {
            return new KnownExtensionConfigElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            var knownExtensionElement = element as KnownExtensionConfigElement;

            if (knownExtensionElement == null)
            {
                throw new ArgumentNullException("element");
            }

            return knownExtensionElement.Pattern;
        }

        #endregion
    }

    /// <summary>
    /// A configuration element of the known extensions configuration collection.
    /// </summary>
    public class KnownExtensionConfigElement : ConfigurationElement
    {
        #region Configuration field names

        private const string c_pattern = "pattern";
        private const string c_defaultKey = "defaultKey";

        #endregion

        /// <summary>
        /// Gets or sets the pattern identifying the extension.
        /// </summary>
        [ConfigurationProperty(c_pattern, IsRequired = true)]
        public string Pattern
        {
            get
            {
                return (string)this[c_pattern];
            }

            set
            {
                this[c_pattern] = value;
            }
        }

        /// <summary>
        /// Gets or sets the default key name for the specific extension.
        /// </summary>
        [ConfigurationProperty(c_defaultKey, IsRequired = false, DefaultValue = "")]
        public string DefaultKey
        {
            get
            {
                return (string)this[c_defaultKey];
            }

            set
            {
                this[c_defaultKey] = value;
            }
        }
    }
}
